package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 添加菜品
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void saveDishWithFlavor(DishDTO dishDTO) {
        //1. 创建pojo并拷贝数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //2. 添加菜品数据到数据库
        dishMapper.save(dish);

        //3. 判断当前的菜品是否有口味数据
        if(dishDTO.getFlavors() != null && dishDTO.getFlavors().size() > 0){
            //4. 如果有口味数据则批量保存口味数据
            dishFlavorMapper.batchInsert(dishDTO.getFlavors(), dish.getId());
        }
    }

    /**
     * 分页查询
     *
     * @param queryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getPageSize());

        Dish dish = new Dish();
        BeanUtils.copyProperties(queryDTO, dish);
        if(null != queryDTO.getCategoryId()){
            dish.setCategoryId(queryDTO.getCategoryId().longValue());
        }


        Page<DishVO> page = dishMapper.pageQuery(dish);

        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     */
    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        ids.forEach(id -> {
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus().equals(StatusConstant.ENABLE)){ //表示当前的菜品属于上架状态
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });

        List<Long> setMealIds = setmealDishMapper.countSetMealByDishId(ids);
        if(null != setMealIds && setMealIds.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        ids.forEach(id ->{
            dishMapper.delete(id);
            dishFlavorMapper.deleteByDishId(id);
        });

    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public DishVO getById(Long id) {
        Dish dish = dishMapper.getById(id);

        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);

        DishVO vo = new DishVO();
        BeanUtils.copyProperties(dish, vo);
        vo.setFlavors(dishFlavors);

        return vo;
    }

    /**
     * 更新菜品数据
     *
     * @param dishDTO
     */
    @Override
    public void update(DishDTO dishDTO) {
        //1. 创建pojo并拷贝数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //2. 更新菜品数据
        dishMapper.update(dish);

        //3. 删除菜品的口味数据
        dishFlavorMapper.deleteByDishId(dish.getId());

        //4. 重新批量插入菜品的口味数据
        if(null != dishDTO.getFlavors() && dishDTO.getFlavors().size() > 0){
            dishFlavorMapper.batchInsert(dishDTO.getFlavors(), dish.getId());
        }
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<DishVO> dishList = dishMapper.pageQuery(dish);

        for (DishVO dishVO : dishList) {
            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(dishVO.getId());

            dishVO.setFlavors(flavors);
        }

        return dishList;
    }
}
